package com.alix.paymoney.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alix.paymoney.R;
import com.alix.paymoney.base.App;
import com.alix.paymoney.base.BaseActivity;
import com.alix.paymoney.base.Url;
import com.alix.paymoney.presenter.CodePresenter;
import com.alix.paymoney.presenter.LoginPresenter;
import com.alix.paymoney.presenter.impl.CodePresenterImpl;
import com.alix.paymoney.presenter.impl.LoginPresenterImpl;
import com.alix.paymoney.utils.EditorShare;
import com.alix.paymoney.utils.Loger;
import com.alix.paymoney.utils.TimerManger;
import com.alix.paymoney.view.CodeView;
import com.alix.paymoney.view.LoginView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity implements CodeView, LoginView {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.get_code)
    TextView getCode;
    @BindView(R.id.login)
    TextView login;
    private String phone, code;
    private TimerManger countDown;
    private CodePresenter codePresenter;
    private LoginPresenter loginPresenter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void afterCreate(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        codePresenter = new CodePresenterImpl(this);
        loginPresenter = new LoginPresenterImpl(this);
        addPhoneListener();
    }

    private void addPhoneListener() {
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                phone = s.toString().trim();
                if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
                    if (countDown == null || !countDown.isRunning()) {
                        getCode.setEnabled(true);
                        Loger.e("可以获取验证码");
                    }
                }
            }
        });
    }

    @OnClick({R.id.back, R.id.login, R.id.get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.get_code:
                codePresenter.getCode(phone);
                break;
            case R.id.login:
                if (TextUtils.isEmpty(phone)) {
                    showSnackBar(etPhone, "请输入手机号码");
                    return;
                }
                if (phone.length() != 11) {
                    showSnackBar(etPhone, "请输入11位手机号码");
                    return;
                }
                if (!matchPhone(phone)) {
                    showSnackBar(etPhone, "请输入正确手机号码");
                    return;
                }
                code = etCode.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    showSnackBar(etPhone, "请输入验证码");
                    return;
                }
                if (code.length() !=5) {
                    showSnackBar(etPhone, "请输入五位验证码");
                    return;
                }
                loginPresenter.login(phone, code);
                break;
        }
    }

    public boolean matchPhone(String phoneNumber) {
        String regExp = "^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147))\\d{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher matcher = p.matcher(phoneNumber);
        return matcher.matches();
    }

    @Override
    public void getCodeSuccess() {
        countDown = new TimerManger(60 * 1000);
        countDown.setOnCountDownListener(new TimerManger.OnCountDownListener() {
            @Override
            public void onTick(long left) {
                getCode.setEnabled(false);
                getCode.setText(left / 1000 > 9 ? left / 1000 + "s" : "0" + left / 1000 + "s");
            }

            @Override
            public void onFinish() {
                getCode.setText("获取验证码");
                if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
                    getCode.setEnabled(true);
                } else {
                    getCode.setEnabled(false);
                }
            }
        });
        countDown.start();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDown != null) {
            countDown.cancel();
        }
    }

    @Override
    public void getCodeError(int errorCode, String errorInfo) {

    }

    @Override
    public void LoginSuccess(String token) {
        App.token = token;
        EditorShare.putString(Url.TOKEN, token);
        launcher(MainActivity.class);
        finish();
    }

    @Override
    public void loginError(int errorCode, String errorInfo) {

    }

    @Override
    public void showTips(String msg) {
        showSnackBar(etPhone, msg);
    }
}
